<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.house_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="选择坐标点"
      ref="gridDialog"
        append-to-body
        :visible.sync="mapBox"
        :fullscreen="false"
        style="width:100%;height:900px;">
      <coordinate-map style="height:600px;" v-on:getGridData="getGridMap($event)" :lngv="form.lng ? form.lng :''" :latv="form.lat ? form.lat :''" />
    </el-dialog>
  </basic-container>
</template>
<script>
  import {getList, getDetail, add, update, remove,getBuildingList,getUnitList,getUnitUnexiList} from "@/api/ninebasic/realpop/house";
  import {mapGetters} from "vuex";
  import { validatenull} from "@/util/validate";
  //打点坐标组件
  import coordinateMap from '@/components/gis/coordinateMap'
  export default {
    data() {
      return {
        mapBox: false,
        form: {
          houseType: '1',
        },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 1250,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          excelBtn: true,
          dialogClickModal: false,
          
          column: [
            {
              label: "所属网格",
              prop: "gridId",
              type: "tree",
              span: 24,
              //multiple: true,
              parent: false,
              display: true,
              
              dicMethod: "get",
              dicUrl: "/api/smart-system/dept/treeByPid",
              props: {
                label: "title"
              
              },
              checkStrictly: true,
              //slot: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入所属网格",
                trigger: "click"
              }],
              change: ({value}) => {
                this.changeGridId(value)
              },
            },
           {
              label: "房屋编号",
              prop: "houseCode",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入房屋编号",
                trigger: "blur"
              }],
            },
            {
              label: "房屋类型",
              prop: "houseType",
              type: "select",
              search: true,
               dicData: [
                {
                  label: "楼房",
                  value: "1"
                },
                {
                  label: "平房",
                  value: "2"
                },
                {
                  label: "别墅",
                  value: "3"
                }
              ],
              rules: [{
                required: true,
                message: "请输入房屋类型",
                trigger: "blur"
              }],
            },
            {
              label: "楼栋",
              prop: "buildingId",
              type: "select",
              hide: true,
              display: true,
              dicData: [],
              props: {
                  label: "title",
                  value: "key"
                },
              rules: [{
                required: true,
                message: "请输入楼栋",
                trigger: "blur"
              }],
               change: ({value}) => {
                this.changeBuildingId(value)
              },
            },
           
            // {
            //   label: "楼号",
            //   prop: "buildingNo",
            //   type: "input",
            //   hide: true,
            //   rules: [{
            //     required: true,
            //     message: "请输入楼号",
            //     trigger: "blur"
            //   }],
            // },
            {
              label: "单元号",
              prop: "buildingUnit",
              type: "select",
              hide: true,
              display: true,
              dicData: [],
              props: {
                  label: "title",
                  value: "key"
                },
              rules: [{
                required: true,
                message: "请输入单元号",
                trigger: "blur"
              }],
               change: ({value}) => {
                this.changeBuildingUnit(value)
              },
            },
             {
              label: "门牌号",
              prop: "houseNumber",
              type: "select",
              dicData: [],
              props: {
                  label: "title",
                  value: "key"
                },
              rules: [{
                required: true,
                message: "请输入门牌号",
                trigger: "blur"
              }],
            },
             {
              label: "小区(村)",
              prop: "communityName",
              type: "input",
              rules: [{
                required: true,
                message: "请输入小区(村)",
                trigger: "blur"
              }],
            },
            {
              label: "房屋地址",
              prop: "houseAddr",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入房屋地址",
                trigger: "blur"
              }],
            },
            {
              label: "房屋状态",
              prop: "houseStatus",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=house_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入房屋状态",
                trigger: "blur"
              }],
            },
            {
              label: "房屋用途",
              prop: "housePurpose",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=house_purpose",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
            },
            {
              label: "房屋面积㎡",
              prop: "houseArea",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入房屋面积㎡",
                trigger: "blur"
              }],
            },
            {
              label: "房主姓名",
              prop: "houseOwnerName",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入房主姓名",
                trigger: "blur"
              }],
            },
            {
              label: "房主联系电话",
              prop: "houseOwnerMobile",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入房主联系电话",
                trigger: "blur"
              }],
            },
            {
              label: "证件类型",
              prop: "certificateType",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=certificate_code",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入证件类型",
                trigger: "blur"
              }],
            },
            {
              label: "证件号码",
              prop: "identificationNumber",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入证件号码",
                trigger: "blur"
              }],
            },
            
            // {
            //   label: "房主身份证号",
            //   prop: "houseOwnerCardNo",
            //   type: "input",
            //   hide: true,
            //   rules: [{
            //     required: true,
            //     message: "请输入房主身份证号",
            //     trigger: "blur"
            //   }],
            // },
            {
              label: "房主现住址",
              prop: "houseOwnerLiveAddr",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入房主现住址",
                trigger: "blur"
              }],
            },
            {
              label: "是否有安全隐患",
              prop: "isDanger",
              type: "radio",
              dicData: [
                {
                  label: "是",
                  value: "2"
                },
                {
                  label: "否",
                  value: "1"
                }
              ],
              hide: true,
              rules: [{
                required: true,
                message: "请输入是否有安全隐患",
                trigger: "blur"
              }],
            },
            {
              label: "安全隐患类型",
              prop: "dangerType",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=danger_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
            },
            {
              label: "是否出租",
              prop: "isRent",
              type: "radio",
              dicData: [
                {
                  label: "是",
                  value: "2"
                },
                {
                  label: "否",
                  value: "1"
                }
              ],
              search: true,
              rules: [{
                required: true,
                message: "请输入是否出租",
                trigger: "blur"
              }],
            },
            {
              label: "出租方式",
              prop: "rentalMode",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "出租用途",
              prop: "rentalPurposes",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=rental_purposes",
              display:true,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入出租用途",
                trigger: "blur"
              }],
            },
            
             {
              label: "承租人姓名",
              prop: "renterName",
              type: "input",
              hide: true,
              display:true,
              rules: [{
                required: true,
                message: "请输入承租人姓名",
                trigger: "blur"
              }],
            },
            {
              label: "承租人联系电话",
              prop: "renterMobile",
              type: "input",
              hide: true,
              display:true,
              rules: [{
                required: true,
                message: "请输入承租人联系电话",
                trigger: "blur"
              }],
            },
            {
              label: "承租人身份证号",
              prop: "renterCardNumber",
              addDisplay: false,
              editDisplay: false,
              display:true,
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入承租人身份证号",
                trigger: "blur"
              }],
            },
           
            {
              label: "是否有中介",
              prop: "isAgency",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "中介名称",
              prop: "agencyName",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "中介联系人",
              prop: "agencyLinkman",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "中介电话",
              prop: "agencyMobile",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            
           {
              label: "经度",
              prop: "lng",
              type: "input",
              hide: true,
              suffixIcon:'el-icon-map-location',
              props: {
                label: "lng",
                value: ""
              },
              // rules: [{
              //   required: false,
              //   message: "请选择坐标点",
              //   trigger: "click"
              // }],
              click:()=>{
                this.openGrid()
              }
            },
            {
              label: "纬度",
              prop: "lat",
              type: "input",
              hide: true,
              suffixIcon:'el-icon-map-location',
              props: {
                label: "lat",
                value: ""
              },
              // rules: [{
              //   required: false,
              //   message: "请选择坐标点",
              //   trigger: "click"
              // }],
              click:()=>{
                this.openGrid()
              }
            },
             {
              label: "备注",
              prop: "remark",
              type: "input",
              span: 24,
              addDisplay: true,
              editDisplay: true,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "空间查询字段",
              prop: "shape",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
          ]
        },
        data: []
      };
    },
    watch: {
      //控制租住信息显示隐藏
      'form.isRent': {
        
        handler(val){
            var rentalPurposes =this.findObject(this.option.column,'rentalPurposes');
            var renterCardNumber =this.findObject(this.option.column,'renterCardNumber');
            var renterName =this.findObject(this.option.column,'renterName');
            var renterMobile =this.findObject(this.option.column,'renterMobile');
            if(val==2){
               rentalPurposes.display =true
              renterCardNumber.display=true
               renterName.display=true;
               renterMobile.display=true;
            }else{
              renterCardNumber.display=false;
              renterName.display=false;
              renterMobile.display=false;
              rentalPurposes.display =false
            }
        },
        
      immediate: true
      },
      'form.houseType': {
         handler(val) {
           var buildingId =this.findObject(this.option.column,'buildingId');
           var buildingUnit =this.findObject(this.option.column,'buildingUnit');
           var houseNumber =this.findObject(this.option.column,'houseNumber');
           if(val==1){
             buildingId.display = true;
             buildingUnit.display = true;
             houseNumber.type = "select"

           }else{
             buildingId.display = false;
             buildingUnit.display = false;
              houseNumber.type = "input"
           }
         },
         immediate: true
      },
    },
    components:{
      coordinateMap
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.house_add, false),
          viewBtn: this.vaildData(this.permission.house_view, false),
          delBtn: this.vaildData(this.permission.house_delete, false),
          editBtn: this.vaildData(this.permission.house_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
       //打开地图图层
        openGrid(){
        this.mapBox = true
      },

      //地图选择组件回调处理数据
      getGridMap(data){
        console.log("callback:",data)

        if(data){
          this.form.lng = data.lng
          this.form.lat = data.lat
        }

        this.mapBox = false
      },
      //网格值改变
      changeGridId(gridId){
          if(!validatenull(gridId)){
                  getBuildingList(this.form.gridId).then(res => {
                      const buildingId = this.findObject(this.option.column, "buildingId");
                      buildingId.dicData = res.data.data;
                  });
                }
      },
       //楼栋
      changeBuildingId(buildingId){
          if(!validatenull(buildingId)){
            getUnitList(buildingId).then(res => {
              // const communityName = this.findObject(this.option.column, "communityName");
                // const houseAddr = this.findObject(this.option.column, "houseAddr");
                // const lng = this.findObject(this.option.column, "lng");
                // const lat = this.findObject(this.option.column, "lat");
                const buildingUnit = this.findObject(this.option.column, "buildingUnit");
                buildingUnit.dicData = res.data.data.mapList;
                this.form.communityName = res.data.data.detail.communityName;
                this.form.houseAddr = res.data.data.detail.addrDetail;
                this.form.lng = res.data.data.detail.lng;
                this.form.lat = res.data.data.detail.lat;
            });
          }
      },
      //单元值改变
       changeBuildingUnit(buildingUnit){
         console.log("1");
          if(!validatenull(buildingUnit)){
            console.log("2");
            getUnitUnexiList(buildingUnit,this.form.buildingId).then(res => {
              console.log(res.data.data)
                const houseNumber = this.findObject(this.option.column, "houseNumber");
                
                houseNumber.dicData = res.data.data;
               
            });
          }
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
.el-form-item__content{
   margin-left: 130px ! important;
}
.el-form-item__label {
  width: 130px ! important;
}
</style>
